package org.dromara.xyyw.controller;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xyyw.domain.bo.XyywCouponBo;
import org.dromara.xyyw.domain.query.XyywCouponQuery;
import org.dromara.xyyw.domain.vo.XyywCouponVo;
import org.dromara.xyyw.service.IXyywCouponService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 优惠券
 *
 * @author yf
 * @date 2024-05-29
 */
@Validated
@RestController
@RequestMapping("/xyyw/coupon")
public class XyywCouponController extends BaseController {

    @Autowired
    private IXyywCouponService xyywCouponService;

    /**
     * 查询优惠券列表
     */
    @SaCheckPermission("xyyw:coupon:list")
    @GetMapping("/list")
    public TableDataInfo<XyywCouponVo> list(XyywCouponQuery query) {
        return xyywCouponService.queryPageList(query);
    }

    /**
     * 导出优惠券列表
     */
    @SaCheckPermission("xyyw:coupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XyywCouponQuery query, HttpServletResponse response) {
        List<XyywCouponVo> list = xyywCouponService.queryList(query);
        ExcelUtil.exportExcel(list, "优惠券", XyywCouponVo.class, response);
    }

    /**
     * 获取优惠券详细信息
     *
     * @param couponId 主键
     */
    @SaCheckPermission(value = {"xyyw:coupon:query", "xyyw:coupon:edit"}, mode = SaMode.OR)
    @GetMapping("/{couponId}")
    public R<XyywCouponVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer couponId) {
        return R.ok(xyywCouponService.queryById(couponId));
    }

    /**
     * 新增优惠券
     */
    @SaCheckPermission("xyyw:coupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XyywCouponBo bo) {
        return toAjax(xyywCouponService.insertByBo(bo));
    }

    /**
     * 修改优惠券
     */
    @SaCheckPermission("xyyw:coupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XyywCouponBo bo) {
        return toAjax(xyywCouponService.updateByBo(bo));
    }

    /**
     * 删除优惠券
     *
     * @param couponIds 主键串
     */
    @SaCheckPermission("xyyw:coupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] couponIds) {
        return toAjax(xyywCouponService.deleteWithValidByIds(List.of(couponIds)));
    }
}
